package fixtures;

import java.io.IOException;
import clientGUI.LoginPanel;
import clientGUI.MainGUI;
import clientGUI.SubscriberGUI;
import server.EchoServer;
import subscriberPanels.UpdateDetailsPanel;
import fit.ActionFixture;

public class OrderABook extends ActionFixture {
	EchoServer server;
	MainGUI main;
	LoginPanel lg;
	UpdateDetailsPanel pn;
	String userName;
	String pw;
	SubscriberGUI sg;
	String newFirstName;
	String newLastName;
	String email;
	String FirstName;
	String LastName;
	String check;
	String phone;
	String id;
	String bid;
	String copy;
	String sid;
	String newPassword;
	String oldPassword;


	
	public void initGui() throws IOException
	{
		main=new MainGUI("localhost",5555);

		MainGUI.testmode=true;
	}
	
	public void startConnection() throws InterruptedException {
		lg=new LoginPanel();
		lg.textFieldLogin.setText(userName);
		String temp=lg.textFieldLogin.getText();
		lg.passwordField.setText(pw);
		MainGUI.done=false;
		lg.btnSubmit.doClick();
		while(MainGUI.done==false);
//		Component x[]=MainGUI.panelButtons.getComponents();
//		int y=MainGUI.panelChanging.getComponentCount();
//		MainGUI.panelButtons.toString();
//		MainGUI.done=false;
//		sg=((SubscriberGUI)x[0]);
//		sg.btnUpdateDetails.doClick();
//		while(MainGUI.done==false);
//		pn=(UpdateDetailsPanel) MainGUI.panelChanging.getComponent(0);
	}
	public void insertUserName(String name) {
		userName=name;
	}
	public void insertPassword(String pass) {
		pw=pass;		
	}

	
	
	public void createTestUserReaderCard()
	{
		String[] s=new String[6];
		s[0]="CreateNewReaderCard";
		s[1]="-1"; 
		s[2]="test";
		s[3]="user";
		s[4]="052222222";
		s[5]="test@user.com";
		userName="-1";
		pw="12345678";
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}
	
	public void enterBid(String bid)
	{
		this.bid=bid;
	}
	
	public void enterSid(String sid)
	{
		this.sid=sid;
	}
	
	public void enterCopy(String copy)
	{
		this.copy=copy;
	}
	
	public void borrow()
	{

		String[] s=new String[4];
		s[0]="BorowBook";
		s[1]=bid;
		s[2]=sid;
		s[3]=copy;
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}
	
	public boolean order() throws InterruptedException
	{
		Thread.sleep(800);
		String msg[]=new String[3];
		msg[0]="OrderBook";
		msg[1]=sid;
		msg[2]=bid;
		MainGUI.OBLClient.handleMessageFromClient(msg);
		while(MainGUI.done==false);
		String[] msgToServer=new String[2];
		msgToServer[0]="ViewBookOrders";
		msgToServer[1]=bid;
		MainGUI.OBLClient.handleMessageFromClient(msgToServer);
		while(MainGUI.done==false);
		switch(MainGUI.ordered)
		{
		case "frozen":
			return false;
		case "full":
			return false;
		case  "exists":
			return false;
		case "orderlist":
			return false;
		case "borrowed":
			return false;
		case "true":
			return true;
		}
		return false;
	}
	public void removeTestUser()
	{
		String[] s=new String[1];
		s[0]="DeleteTestUsers";
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}
	
	public void returnBook()
	{
		String[] s=new String[3];
		s[0]="ReturnABook";
		s[1]=bid;
		s[2]=copy;
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}
	
	
	public void freeze()
	{
		String[] s=new String[2];
		s[0]="Freeze";
		s[1]=sid;
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}
	public void unFreeze()
	{
		String[] s=new String[2];
		s[0]="Freeze";
		s[1]=sid;
		MainGUI.OBLClient.handleMessageFromClient(s);
		while(MainGUI.done==false);
	}
	
	

}
